package pl.register.android.database.crud;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import pl.register.android.database.dao.StudentGroupDao;
import pl.register.android.database.tables.StudentGroupDBTable;
import pl.register.android.database.tables.StudentInGroupsDBTable;
import pl.register.android.group.GroupType;
import pl.register.android.group.StudentsGroup;
import pl.register.android.student.Student;
import pl.register.android.year.Year;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-26
 */
public class StudentGroupCRUD implements StudentGroupDao {

    private SQLiteDatabase database;

    private StudentInGroupCRUD studentInGroupCRUD;

    public StudentGroupCRUD(SQLiteDatabase database) {
        this.database = database;
        studentInGroupCRUD = new StudentInGroupCRUD(database);
    }

    @Override
    public List<StudentsGroup> findGroupsBy(Year year) {
        List<StudentsGroup> studentsGroups = new ArrayList<StudentsGroup>();
        String where = StudentGroupDBTable.FK_YEAR_ID + " = " + year.getId();
        Cursor cursor = database.query(StudentGroupDBTable.TABLE_NAME, null, where, null, null, null, null);
        while (cursor.moveToNext()) {
            Long groupId = cursor.getLong(cursor.getColumnIndex(StudentGroupDBTable.ID));
            String groupTypeString = cursor.getString(cursor.getColumnIndex(StudentGroupDBTable.TYPE));
            GroupType groupType = GroupType.valueOf(groupTypeString);
            studentsGroups.add(new StudentsGroup(groupId, groupType));
        }
        cursor.close();
        return studentsGroups;
    }

    @Override
    public StudentsGroup findGroupsBy(Student student, Year year) {
        StudentsGroup group = null;
        String query = "SELECT " +
                StudentGroupDBTable.STUDENTGROUP_ID + ", " +
                StudentGroupDBTable.STUDENTGROUP_TYPE +
                " FROM " + StudentGroupDBTable.TABLE_NAME +
                " INNER JOIN " + StudentInGroupsDBTable.TABLE_NAME +
                " ON " + StudentGroupDBTable.STUDENTGROUP_ID + " = " + StudentInGroupsDBTable.STUDENTINGROUP_FK_GROUP_ID +
                " WHERE " + StudentGroupDBTable.STUDENTGROUP_FK_YEAR_ID + " = " + year.getId() +
                " AND " + StudentInGroupsDBTable.STUDENTINGROUP_FK_STUDENT_ID + " = " + student.getId() + ";";
        Log.d("TAG", " get student group,  query: " + query);
        Cursor cursor = database.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            Long groupId = cursor.getLong(cursor.getColumnIndex(StudentGroupDBTable.ID));
            String groupTypeString = cursor.getString(cursor.getColumnIndex(StudentGroupDBTable.TYPE));
            GroupType groupType = GroupType.valueOf(groupTypeString);
            group = new StudentsGroup(groupId, groupType);
        }
        cursor.close();
        return group;
    }

    @Override
    public StudentsGroup insertGroup(StudentsGroup studentsGroup, Year year) {
        ContentValues insertValues = new ContentValues();
        insertValues.put(StudentGroupDBTable.TYPE, studentsGroup.getType().toString());
        insertValues.put(StudentGroupDBTable.FK_YEAR_ID, year.getId());
        long res = database.insert(StudentGroupDBTable.TABLE_NAME, null, insertValues);
        studentsGroup.setId(res);
        return studentsGroup;
    }

    @Override
    public void addStudentTo(Student student, StudentsGroup group) {
        studentInGroupCRUD.insert(student, group);
    }

    @Override
    public void deleteStudentFrom(Student student, StudentsGroup group) {
        studentInGroupCRUD.delete(student, group);
    }

}
